var text;

_init();

function _assert(a, comment) {
    return a ? success(comment) : fail(comment);
}

function assert(a) {
    if (typeof(a) != 'boolean') {
        fail("Invalid argument type " +typeof(a) +" to assert(boolean)");
    } else {
        _assert(a === true, "Call to assert(boolean)");
    }
}

function assertTrue(a) {
    if (typeof(a) != 'boolean') {
        fail("Invalid argument type " +typeof(a) +" to assertTrue(boolean)");
    } else {
        _assert(a === true, "Call to assertTrue(boolean)");
    }
}

function assertFalse(a) {
    if (typeof(a) != 'boolean') {
        fail("Invalid argument type" +typeof(a) +" to assertFalse(boolean)");
    } else {
        _assert(a !== true, "Call to assertFalse(boolean)");
    }
}

function assertEquals(a, b) {
    _assert(a === b, "Call to assertEquals(" +a +"," +b +")");
}

function assertNotEquals(a, b) {
    _assert(a !== b, "Call to assertNotEquals(" +a +"," +b +")");
}

function assertNull(a) {
    _assert(a === null, "Call to assertNull(" +a +")");
}

function assertNotNull(a) {
    _assert(a !== null, "Call to assertNotNull(" +a +")");
}

function assertUndefined(a) {
    _assert(a === undefined, "Call to assertUndefined(" +a +")");
}

function assertNotUndefined(a) {
    _assert(a !== undefined, "Call to assertNotUndefined(" +a +")");
}

function assertNaN(a) {
    _assert(isNaN(a), "Call to assertNaN(" +a +")");
}

function assertNotNaN(a) {
    _assert(!isNaN(a), "Call to assertNotNaN(" +a +")");
}

function fail(comment) {
	var line = new SVGElement("tspan", { x: 20, dy: "1.5em", stroke: "red" });
	line.instance.textContent = "FAIL: " +comment;
	text.add(line);
	
//    throw new Exception(comment);
}

function success(comment) {
	var line = new SVGElement("tspan", { x: 20, dy: "1.5em", stroke: "green" });
	line.instance.textContent = "Success: " +comment;
	text.add(line);
	
}

function TestCase() {
	this.setUp = function() {}
	this.tearDown = function() {}
	
	this.run = function() {
		for (var i in this) {
			this.setUp();
			if (this[i] != null && typeof(this[i]) === 'function' && i.substring(0,4) === 'test') {
				this[i].apply(this, null);
			}
			this.tearDown();
		}
	}
}

function TestSuite() {
	this.tests = new Array();
	
	this.addTest = function(testCase) {
		this.tests.add(testCase);
	}
	
	this.run = function() {
		for (var i in this.tests) {
			this.tests[i].run();
		}
	}
}

function SVGElement(tag, attrs) {
	this.tag = tag;
	this.attrs = attrs;
	this.instance = document.createElement(tag);
  
	for (attr in this.attrs) {
		this.instance.setAttribute(attr, this.attrs[attr]);
	}

	this.add = function(element) {
		this.instance.appendChild(element.instance); 
	}
}

function _init() {
	var bg = new SVGElement("g", { transform: "translate(20 20)" });
	
	bg.add(new SVGElement("rect", { x: 0, y:0, rx: 10, ry: 10, width: 600, height: 300, fill: "#cccccc", opacity: 0.75 }));
	
	text = new SVGElement("textArea", { x: 20, y: 0, stroke: "black" });
	
	bg.add(text);
	
	document.documentElement.appendChild(bg.instance);
}